package controllers.admin;

import play.*;

import play.mvc.*;
import service.TransPriceService;

import java.util.*;

import controllers.Secure;

import models.*;
import models.BaseEntity.Status;
import models.Message.MessageStatus;
import models.Message.MessageType;

@With(Secure.class)
public class TransPrices extends Controller {
		
	/**
	 * 进入运价管理页面
	 */
	public static void indexTransfer(QueryBean queryBean){
		List<TransferCompany> companyList = TransferCompany.findAll();
		List<Province> provinceList = Province.findAll();
		
		if(queryBean == null){
			queryBean = new QueryBean();
		}
		
		render(queryBean,companyList,provinceList);
	}
	
	/**
	 * 异步加载城市列表
	 * 
	 * @param provid
	 * @return
	 */
	public static String getCityListByProvince(long provid){
		
		Province pro = Province.findById(provid);
		List<City> cityList = pro.cityList;
		
		String result = "";
		if(cityList!=null){
			for(City city : cityList){
				result = city.id.toString()+"|"+city.cityName+"#";
			}
		}
		
		return result;
	}
	
	/**
	 * 查询公司运价信息
	 */
	public static void queryTransPrice(QueryBean queryBean,long companyId,long starProvinceId,
			long starCityId,long endProvinceId,long endCityId){
		
		List<Province> provinceList = Province.findAll();
		List<TransferCompany> companyList = TransferCompany.findAll();
		
		StringBuilder hql = new StringBuilder("from TransferPrice tp where 1=1 ");
		if(companyId != 0){
			hql.append(" and tp.company.id="+companyId);
		}
		
		if(starCityId != 0){
			hql.append(" and tp.startCity.id="+starCityId);
		}
		
		if(endCityId != 0){
			hql.append(" and tp.endCity.id="+endCityId);
		}
		
		if(starProvinceId != 0){
			hql.append(" and tp.startCity.province.id="+starProvinceId);
		}
		
		if(endProvinceId != 0){
			hql.append(" and tp.endCity.province.id="+endProvinceId);
		}
		
		hql.append("order by tp.company.id");
		
		if(queryBean == null){
			queryBean = new QueryBean();
		}
		
		//页数计算
		queryBean.totalQuantity = Message.count(hql.toString(), new Object[]{});
		queryBean.totalPage = (queryBean.totalQuantity/queryBean.quantityEachPage + 1);
		if(queryBean.totalPage < queryBean.currentPage) {
			queryBean.currentPage = queryBean.totalPage;
		}
		
		//查询结果
		List<TransferPrice> priceList = TransferPrice.find(hql.toString()).fetch(
				(int)queryBean.currentPage, queryBean.quantityEachPage);
		
		render("@indexTransfer",queryBean,priceList,provinceList,companyList);
	}
	
	/**
	 * 创建公司的运价
	 * 
	 * @param companyIdVal
	 * @param startProvIdVal
	 * @param startCityIdVal
	 * @param endProvIdVal
	 * @param endCityIdVal
	 * @param publicBasePriceVal
	 * @param publicRenewalPriceVal
	 * @param privateBasePriceVal
	 * @param privateRenewalPriceVal
	 */
	public static String createTransPrice(long companyIdVal,long startProvIdVal,
			long startCityIdVal,long endProvIdVal,long endCityIdVal,
			Double publicBasePriceVal,Double publicRenewalPriceVal,
			Double privateBasePriceVal,Double privateRenewalPriceVal,
			Double publicBaseWeight,Double privateBaseWeight)
	{
		//检查运价是否已经存在		
		StringBuilder hql = new StringBuilder("from TransferPrice tp where 1=1 ");
		
		if(companyIdVal != 0){
			hql.append(" and tp.company.id="+companyIdVal);
		}
		
		if(startCityIdVal != 0){
			hql.append(" and tp.startCity.id="+startCityIdVal);
		}
		
		if(endCityIdVal != 0){
			hql.append(" and tp.endCity.id="+endCityIdVal);
		}
		
		String result = "SUCCESS";
		
		List<TransferPrice> priceList = TransferPrice.find(hql.toString()).fetch();
		if(priceList != null && priceList.size()>0){
			result = "DUPLICATE";
		}else{
			TransferCompany company = TransferCompany.findById(companyIdVal);
			City startCity = City.findById(startCityIdVal);
			City endCity = City.findById(endProvIdVal);
			
			TransferPrice tp = new TransferPrice();
			tp.startCity = startCity;
			tp.endCity = endCity;
			tp.company = company;
			tp.publicBasePrice = publicBasePriceVal;
			tp.publicRenewalPrice = publicRenewalPriceVal;
			tp.privateBasePrice = privateBasePriceVal;
			tp.privateRenewalPrice = privateRenewalPriceVal;
			tp.publicBaseWeight = publicBaseWeight;
			tp.privateBaseWeight = privateBaseWeight;
			
			tp.save();
		}
		
		return result;
		
	}
	
	/**
	 * 删除运价
	 * 
	 * @param transPriceId
	 */
	public static String deleteTransPrice(long transPriceId){
		TransferPrice tp = TransferPrice.findById(transPriceId);
		tp.startCity = null;
		tp.endCity = null;
		tp.company = null;
		tp.delete();
		
		return "SUCCESS";
	}
	
	/**
	 * 获取运价
	 * 
	 * @param transPriceId
	 */
	public static String getTransPrice(long transPriceId){
		TransferPrice tp = TransferPrice.findById(transPriceId);
		StringBuilder result = new StringBuilder();
		result.append("companyId-update:"+tp.company.companyName);
		result.append("#");
		result.append("starProvinceId-update:"+tp.startCity.province.name);
		result.append("#");
		result.append("starCityId-update:"+tp.startCity.cityName);
		result.append("#");
		result.append("endProvinceId-update:"+tp.endCity.province.name);
		result.append("#");
		result.append("endCityId-update:"+tp.endCity.cityName);
		result.append("#");
		result.append("publicBasePrice-update:"+tp.publicBasePrice);
		result.append("#");
		result.append("publicRenewalPrice-update:"+tp.publicRenewalPrice);
		result.append("#");
		result.append("privateBasePrice-update:"+tp.privateBasePrice);
		result.append("#");
		result.append("privateRenewalPrice-update:"+tp.privateRenewalPrice);
		result.append("#");
		result.append("publicBaseWeight-update:"+tp.publicBaseWeight);
		result.append("#");
		result.append("privateBaseWeight-update:"+tp.privateBaseWeight);
		
		return result.toString();
	}
	
	public static String updateTransPrice(long transPriceId,
			Double publicBasePriceVal,Double publicRenewalPriceVal,
			Double privateBasePriceVal,Double privateRenewalPriceVal,
			Double publicBaseWeight,Double privateBaseWeight)
	{
		TransferPrice tp = TransferPrice.findById(transPriceId);
		tp.publicBasePrice = publicBasePriceVal;
		tp.publicRenewalPrice = publicRenewalPriceVal;
		tp.publicBaseWeight = publicBaseWeight;
		tp.privateBasePrice = privateBasePriceVal;
		tp.privateRenewalPrice = privateRenewalPriceVal;
		tp.privateBaseWeight = privateBaseWeight;
		
		tp.save();
		
		return "SUCCESS";
	}
    
}

